package com.fx.eduservice.controller;


import com.fx.commonuntils.ErrorEnum;
import com.fx.commonuntils.ReturnMessage;
import com.fx.commonuntils.ReturnMessageUtils;
import com.fx.eduservice.entity.vo.EduChapter;
import com.fx.eduservice.entity.vo.EduCourse;
import com.fx.eduservice.service.EduChapterService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Objects;



/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-11-07
 */
@Api(description = "课程章系统")
@RestController
@RequestMapping("/eduservice/eduChapter")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@Slf4j
@CrossOrigin
public class EduChapterController {

    final private EduChapterService eduChapterService;

    @ApiOperation(value = "通过课程id获取章节信息")
    @GetMapping("/getChapterVideo/{couseId}")
    public ReturnMessage<Object> getChapterVideo(@ApiParam(name = "couseId", value = "课程id", required = true)
                                                 @PathVariable String couseId) {

        return eduChapterService.getChapterVideo(couseId);


    }

    @ApiOperation(value = "通过章id获取章信息")
    @GetMapping("/getChapterById/{chapterId}")
    public ReturnMessage<EduChapter> getChapterById(@ApiParam(name = "chapterId", value = "课程id", required = true)
                                                 @PathVariable String chapterId) {
        EduChapter chapterById = eduChapterService.getById(chapterId);

        return ReturnMessageUtils.success(chapterById);


    }


    @ApiOperation(value = "新增、修改章")
    @PostMapping("/addChapter")
    public ReturnMessage<Object> addChapter(@RequestBody @Valid EduChapter eduChapter, BindingResult br) {
        log.info("Request:{}", eduChapter);
        if (eduChapter == null) {
            return ReturnMessageUtils.errorWithEum(ErrorEnum.E_REQUEST_PARAM_ERRORJSON);
        }
        if (br.hasErrors()) {
            return ReturnMessageUtils.error(ErrorEnum.E_REQUEST_PARAM_ERROR.getErrorCode(), Objects.requireNonNull(br.getFieldError()).getDefaultMessage());
        }

        boolean b = eduChapterService.saveOrUpdate(eduChapter);
        if (b) {
            return ReturnMessageUtils.success("新增章节成功", eduChapter.getId());
        } else {
            return ReturnMessageUtils.error(ErrorEnum.E_EDU_TEACHER.getErrorCode(), ErrorEnum.E_EDU_TEACHER.getErrorMsg(), b);
        }

    }

    @ApiOperation(value = "删除章")
    @PostMapping("/deleteChapter")
    public ReturnMessage<Object> deleteChapter(@RequestBody @Valid EduChapter eduChapter, BindingResult br) {
        log.info("Request:{}", eduChapter);
        if (eduChapter == null) {
            return ReturnMessageUtils.errorWithEum(ErrorEnum.E_REQUEST_PARAM_ERRORJSON);
        }
        if (br.hasErrors()) {
            return ReturnMessageUtils.error(ErrorEnum.E_REQUEST_PARAM_ERROR.getErrorCode(), Objects.requireNonNull(br.getFieldError()).getDefaultMessage());
        }

        return eduChapterService.deleteById(eduChapter);


    }


}